package com.psw.monitor.geserialport.service;

import cn.hutool.json.JSONUtil;
import com.psw.monitor.geserialport.model.SerialPortMessage;
import com.psw.monitor.geserialport.utils.ConvertHexStrAndStrUtils;

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class MessageReader {

    public static SerialPortMessage readHexStr(String str) {
        byte[] bytes = ConvertHexStrAndStrUtils.hexStrToBytes(str);
        return read(bytes);
    }

    public static SerialPortMessage read(byte[] bytes) {

        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        SerialPortMessage message = new SerialPortMessage();
        byte[] headers1 = new byte[2];
        headers1[0] = byteBuffer.get();
        headers1[1] = byteBuffer.get();
        message.setHeader(headers1);
        byte[] peerAddr = new byte[6];
        peerAddr[0] = byteBuffer.get();
        peerAddr[1] = byteBuffer.get();
        peerAddr[2] = byteBuffer.get();
        peerAddr[3] = byteBuffer.get();
        peerAddr[4] = byteBuffer.get();
        peerAddr[5] = byteBuffer.get();
        message.setPeerAddr(peerAddr);
        byte[] addr = new byte[6];
        addr[0] = byteBuffer.get();
        addr[1] = byteBuffer.get();
        addr[2] = byteBuffer.get();
        addr[3] = byteBuffer.get();
        addr[4] = byteBuffer.get();
        addr[5] = byteBuffer.get();
        message.setAddr(addr);
        // 2补位
        byteBuffer.get();
        byteBuffer.get();
        message.setRssi(byteBuffer.getInt());
        message.setTicks(byteBuffer.getInt());
        SerialPortMessage.ScanResponseData scanResponseData = new SerialPortMessage.ScanResponseData();
        byte[] header = new byte[2];
        header[0] = byteBuffer.get();
        header[1] = byteBuffer.get();
        scanResponseData.setHeader(header);
        char[] str = new char[7];
        str[0] = (char) byteBuffer.get();
        str[1] = (char) byteBuffer.get();
        str[2] = (char) byteBuffer.get();
        str[3] = (char) byteBuffer.get();
        str[4] = (char) byteBuffer.get();
        str[5] = (char) byteBuffer.get();
        str[6] = (char) byteBuffer.get();
        scanResponseData.setStr(str);
        //补位1
        byteBuffer.get();
        SerialPortMessage.TempHumidData thd1 = new SerialPortMessage.TempHumidData();
        thd1.setTemperature(new BigDecimal(getUnsignedShort(byteBuffer)).divide(new BigDecimal("10"),1,BigDecimal.ROUND_HALF_UP));
        thd1.setHumidity(new BigDecimal(getUnsignedShort(byteBuffer)).divide(new BigDecimal("10"),1,BigDecimal.ROUND_HALF_UP));
        SerialPortMessage.TempHumidData thd2 = new SerialPortMessage.TempHumidData();
        thd2.setTemperature(new BigDecimal(getUnsignedShort(byteBuffer)).divide(new BigDecimal("10"),1,BigDecimal.ROUND_HALF_UP));
        thd2.setHumidity(new BigDecimal(getUnsignedShort(byteBuffer)).divide(new BigDecimal("10"),1,BigDecimal.ROUND_HALF_UP));

        SerialPortMessage.TempHumidData[] thds = new SerialPortMessage.TempHumidData[2];
        thds[0] = thd1;
        thds[1] = thd2;
        scanResponseData.setTempHumidData(thds);
        SerialPortMessage.SGP30Data sgp30Data = new SerialPortMessage.SGP30Data();
        sgp30Data.setEco2(byteBuffer.getShort());
        sgp30Data.setTvoc(byteBuffer.getShort());
        scanResponseData.setSgp30Data(sgp30Data);

        byte[] ioData = new byte[4];
        ioData[0] = byteBuffer.get();
        ioData[1] = byteBuffer.get();
        ioData[2] = byteBuffer.get();
        ioData[3] = byteBuffer.get();
        scanResponseData.setIoData(ioData);

        short[] adcData = new short[3];
        adcData[0] = byteBuffer.getShort();
        adcData[1] = byteBuffer.getShort();
        adcData[2] = byteBuffer.getShort();
        scanResponseData.setAdcData(adcData);

        byte[] reserved = new byte[14];
        reserved[0] = byteBuffer.get();
        reserved[1] = byteBuffer.get();
        reserved[2] = byteBuffer.get();
        reserved[3] = byteBuffer.get();
        scanResponseData.setReserved(reserved);

        byte[] randomValue = new byte[16];
        randomValue[0] = byteBuffer.get();
        randomValue[1] = byteBuffer.get();
        randomValue[2] = byteBuffer.get();
        randomValue[3] = byteBuffer.get();
        randomValue[4] = byteBuffer.get();
        randomValue[5] = byteBuffer.get();
        randomValue[6] = byteBuffer.get();
        randomValue[7] = byteBuffer.get();
        randomValue[8] = byteBuffer.get();
        randomValue[9] = byteBuffer.get();
        randomValue[10] = byteBuffer.get();
        randomValue[11] = byteBuffer.get();
        randomValue[12] = byteBuffer.get();
        randomValue[13] = byteBuffer.get();
        randomValue[14] = byteBuffer.get();
        randomValue[15] = byteBuffer.get();
        scanResponseData.setRandomValue(randomValue);
        message.setScanresponsedata(scanResponseData);
        byte[]footer = new byte[2];
        footer[0] = byteBuffer.get();
        footer[1] = byteBuffer.get();
        message.setFooter(footer);
        return message;
    }

    public static int getUnsignedShort(ByteBuffer buff) {
        return buff.getShort() & 0xffff;
    }


        public static void main(String[] args) {
        SerialPortMessage msg = MessageReader.readHexStr("AABB8ABE18A6EDDC788CFF1775F70000E7FFFFFF1D1C7E00999847455F43434D0000BE004F01000000000402D30000000000AED1482D000000000000780D3E8C3001FFF21718BF7568F79839CCDD0000");
        System.out.println(JSONUtil.toJsonStr(msg));
    }
}
